package xin.marcher.module.rights.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.rights.api.LoginApi;
import xin.marcher.module.rights.domain.dto.LoginResultDTO;
import xin.marcher.module.rights.domain.request.CheckTokenRequest;
import xin.marcher.module.rights.domain.request.LoginRequest;
import xin.marcher.module.rights.service.LoginService;

/**
 *
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = LoginApi.class, retries = 0)
public class LoginApiImpl implements LoginApi {

    @Autowired
    private LoginService loginService;

    @Override
    public JsonResult login(LoginRequest request) {
        try {
            LoginResultDTO loginResultDTO = loginService.login(request);
            return JsonResult.buildSuccess(loginResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult checkToken(CheckTokenRequest request) {
        try {
            return loginService.checkToken(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
